package com.pronhub.luowowo.comment.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.pronhub.luowowo.comment.domain.TravelComment;
import com.pronhub.luowowo.comment.query.TravelCommentQuery;
import com.pronhub.luowowo.comment.repository.ITravelCommentRepository;
import com.pronhub.luowowo.comment.service.ITravelCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {

    @Autowired
    private ITravelCommentRepository repository; //简单的
    @Autowired
    private MongoTemplate template;

    @Override
    public TravelComment saveOrUpdate(TravelComment comment) {
        //添加或者更新
        if (!StringUtils.hasLength(comment.getId())){
            comment.setCreateTime(new Date());
        }

        //二级评论
        if (comment.getType()==TravelComment.TRAVLE_COMMENT_TYPE){
            if (StringUtils.hasLength(comment.getRefComment().getId())){
                //如果是二级评论 往评论里面设置评论
                    //todo 理解 二级评论
                comment.setRefComment(repository.findById(comment.getRefComment().getId()).get());
            }
        }
        template.save(comment);
        return comment;
    }

    @Override
    public Page query(TravelCommentQuery qo) {
        //查询条件的创建
        Query query = new Query();
        //根据文章的travelId来查
        query.addCriteria(Criteria.where("travelId").is(qo.getTravelId()));
        //查总数
        Long count = template.count(query, TravelComment.class);

        if (count==0){
            return Page.empty();
        }

        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.by(Sort.Direction.DESC, "createTime"));
        query.with(pageable);

        //查某一页  List
        List<TravelComment> list = template.find(query, TravelComment.class);
        // PageImpl  Page接口实现类
        return new PageImpl(list, pageable,count );
    }

    @Override
    public Long getcountByTravelId(Long travelId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("travelId").is(travelId));
        long count = template.count(query, TravelComment.class);
        return count;
    }

    @Override
    public List<TravelComment> queryForCreatTimeTop10() {
        Query query = new Query();
        query.with(Sort.by(Sort.Direction.DESC, "createTime"));
        query.skip(0).limit(10);
        List<TravelComment> list = template.find(query, TravelComment.class);
        return list;
    }
}
